﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CalendarAPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            frominit();
            contrlRefresh();
        }

        #region
        private void btnYearAdd_Click(object sender, EventArgs e)
        {
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddYears(1);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void btnYearSub_Click(object sender, EventArgs e)
        {
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddYears(-1);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void btnMonthAdd_Click(object sender, EventArgs e)
        {
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddMonths(1);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void btnMonthSub_Click(object sender, EventArgs e)
        {
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddMonths(-1);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void btnCurrent_Click(object sender, EventArgs e)
        {
            myCalendarControl1.CurrentDate = DateTime.Now;
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void cmbYear_TextChanged(object sender, EventArgs e)
        {
            if (cmbYear.Text == string.Empty) return;
            int year = myCalendarControl1.CurrentDate.Year;
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddYears(Int32.Parse(cmbYear.Text) - year);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }

        private void cmbMoth_TextChanged(object sender, EventArgs e)
        {
            if (cmbMoth.Text == string.Empty) return;
            int moth = myCalendarControl1.CurrentDate.Month;
            myCalendarControl1.CurrentDate = myCalendarControl1.CurrentDate.AddMonths(Int32.Parse(cmbMoth.Text) - moth);
            myCalendarControl1.Refresh();
            contrlRefresh();
        }
        #endregion

        #region
        private void frominit()
        {
            myCalendarControl1.CurrentDate = DateTime.Now;
            System.Globalization.ChineseLunisolarCalendar calendar = new System.Globalization.ChineseLunisolarCalendar();
            int maxyear = calendar.MaxSupportedDateTime.Year;
            int minyear = calendar.MinSupportedDateTime.Year;
            for (int i = minyear; i <= maxyear; i++)
            {
                cmbYear.Items.Add(i.ToString());
            }
            for (int i = 1; i < 13; i++)
            {
                cmbMoth.Items.Add(i.ToString());
            }
        }

        private void contrlRefresh()
        {
            cmbYear.Text = myCalendarControl1.CurrentDate.Year.ToString();
            cmbMoth.Text = myCalendarControl1.CurrentDate.Month.ToString();

            lblLunisolarDate.Text = string.Format("农历 {0}年", myCalendarControl1.LunisolarDate);
        }
        #endregion

        

        
    }
}
